﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using MINERP.BLL.InfoConfig;
using MINERP.DAL;
using MINERP.MODEL.InfoConfig;
using MINERP.COM;

namespace MINERP.InfoConfig.Controller
{
    public class AreaController
    {
        #region //private fields
        private AreaBLL _bll = new AreaBLL();
        private GridControl _grid = null;
        private GridView _view = null;
        private ComboBoxEdit _comboBox = null;
        private GroupControl _groupControl = null;
        private CheckEdit _checkEdit = null;

        #endregion


        #region //Public Properties

        internal GroupControl GroupControl
        {
            set
            {
                _groupControl = value;
            }
        }
        internal CheckEdit CheckEdit
        {
            set
            {
                _checkEdit = value;
            }
        }
        internal ComboBoxEdit ComoBox
        {
            set
            {
                _comboBox = value;
            }
        }
        internal GridControl GridControl
        {
            set
            {
                _grid = value;
            }
        }

        internal GridView View
        {
            set
            {
                this._view = value;
            }
            get
            {
                return _view;
            }
        }

        #endregion

        #region //Public Methods

        /// <summary>
        /// 绑定 gridControl
        /// </summary>
        public void GetDate()
        {
            _grid.DataSource = _bll.GetAreaList();
        }
        /// <summary>
        /// 获得单个Area对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public GridSubArea GetAreaObj(int id)
        {
            return _bll.GetAreaObj(id);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="list"></param>
        public void Add(GridSubArea obj)
        {
            for (int i = 1; i <= obj.widthNum * obj.heightNum; i++)
            {
                Grid grid = new Grid();
                grid.GridName = obj.nameHeadChar.ToUpper().Trim() + i.ToString();
                grid.FirstEnterTime = DateTime.Now;
                grid.SomeId = 0;
                grid.Status = Convert.ToInt32(GridStatus.useAble);
                obj.Grid.Add(grid);
            }
            _bll.Add(obj);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="obj"></param>
        public void Update(GridSubArea area)
        {
            //GridSubArea obj = new GridSubArea();
            //obj.GridClearTime = area.GridClearTime;
            //obj.heightNum = area.heightNum;
            //obj.isUseful = area.isUseful;
            //obj.nameHeadChar = area.nameHeadChar;
            //obj.OutTimeAlarmTime = area.OutTimeAlarmTime;
            //obj.SubAreaName = area.SubAreaName;
            //obj.Type = area.Type;
            //obj.widthNum = area.widthNum;
            //if (!area.Grid.IsLoaded)
            //{
            //    area.Grid.Load();
            //}
            //foreach (var item in area.Grid)
            //{
            //    //if (!item.GridRecord.IsLoaded)
            //    //{
            //    //    item.GridRecord.Load();
            //    //}
            //    //if (!item.GridRecordHistory.IsLoaded)
            //    //{
            //    //    item.GridRecordHistory.Load();
            //    //}
            //    //item.GridRecord.Clear();
            //    //item.GridRecordHistory.Clear();
                
            //}
            //area.Grid.Clear();
            _bll.DeleteGrid(area);
            //_bll.Update();
            for (int i = 1; i <= area.widthNum * area.heightNum; i++)
            {
                Grid grid = new Grid();
                grid.GridName = area.nameHeadChar.ToUpper().Trim() + i.ToString();
                grid.FirstEnterTime = DateTime.Now;
                grid.SomeId = 0;
                grid.Status = Convert.ToInt32(GridStatus.useAble);
                area.Grid.Add(grid);
            }
            _bll.Update();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="obj"></param>
        public void Delete(GridSubArea obj)
        {
            _bll.Delete(obj);
        }

        public void LinkAreaType()
        {
            _comboBox.Properties.Items.Add(SubAreaType.Shop);
            _comboBox.Properties.Items.Add(SubAreaType.Photo);
            _comboBox.Properties.Items.Add(SubAreaType.OutShop);
        }
        #endregion
    }
}
